import {pinglunBehavior} from './behavior'
import {toast} from '../../utils/extendApi'
import {addComment} from '../../api/comment'

// pages/pinglun/pinglun.js
Page({

  behaviors: [pinglunBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [

    ],
    rateList: [
      {
        name: '总体',
        rate: 'total'
      },{
        name: '包装',
        rate: 'packaging'
      },{
        name: '味道',
        rate: 'flavor'
      }
    ],
    commentForm: {
      rate: {
        flavor: 0,
        packaging: 0,
        total: 0
      },
      content: ''
    }
  },
  // 修改评分
  onChange(event) {
    // console.log(event,111);
    this.data.commentForm.rate[event.currentTarget.dataset.rate] = event.detail
  },

  // 修改内容
  changeContent(event) {
    console.log(event,1111);
    this.setData({
      "commentForm.content": event.detail.value
    })
  },

  // 评论发送
  async sendComment() {
    this.data.commentForm.rate = JSON.stringify(this.data.commentForm.rate)
    this.data.commentForm.userId = this.data.order.userid
    this.data.commentForm.storeId = this.data.order.storeid
    this.data.commentForm.orderId = this.data.order.id

    console.log(this.data.commentForm);
    const res = await addComment(this.data.commentForm)
    wx.navigateBack({
      complete() {
        if (res.code === 200) {
          toast({
            title: '评论成功！',
            icon: 'success',
            mask: false
          })
        }
      }
    })
  }

})